<template>
    <div v-loading="loading">
        <p class="title">查询可用余额</p>
        <el-form ref="ruleForm" :model="ruleForm" :rules="rules">
            <el-row :gutter="1">
                <el-col :span="24">
                    <el-form-item label="地址 :" prop="address" :label-width="formLabelWidth3">
                        <el-input size="small" v-model="ruleForm.address"></el-input>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1">
                <el-col :span="24">
                    <el-form-item label="" :label-width="formLabelWidth3">
                        <el-button type="primary" size="small" @click="searchNew">新查询</el-button>
                        <el-button type="primary" size="small" @click="search">继续查询</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div class="line"></div>
        <p class="title">交易输入</p>
        <el-form class="form-label-bottom" ref="ruleForm1">
            <div v-for="(item,index) in showChooseLists">
                <div style="display: inline-block;width: 75%">
                    <el-row :gutter="1">
                        <el-col :span="24">
                            <el-form-item label="交易ID :" prop="txId" :label-width="formLabelWidth3">
                                <el-input size="small" v-model="item.txid"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="24">
                            <el-form-item label="地址 :" prop="index" :label-width="formLabelWidth3">
                                <el-input size="small" v-model="item.address"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="24">
                            <el-form-item :label="'数量'+ (proParm ==='fch' ? 'f':'') + ':'" prop="amount"
                                          :label-width="formLabelWidth3">
                                <el-input size="small" v-model="item.amount"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="24">
                            <el-form-item label="索引 :" prop="index" :label-width="formLabelWidth3">
                                <el-input size="small" v-model="item.vout"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div style="display: inline-block;width: 20%;text-align: left;vertical-align: top;margin-top: 5px">
                    <el-button class="block_btn" type="primary" size="small" @click="del(index)">删除</el-button>
                    <span class="indexCode">编号: {{index + 1}}</span>
                    <!--<i class="erweima" @click="erweimaClick(index)"></i>-->
                </div>
                <div class="line"></div>
            </div>
            <el-row :gutter="1">
                <el-col :span="24">
                    <el-form-item label="" :label-width="formLabelWidth3">
                        <el-button type="primary" size="small" @click="addTransactionIn">添加交易输入</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
        <div class="line"></div>
        <p class="title">交易输出</p>
        <el-form class="form-label-bottom" ref="ruleForm2">
            <div v-for="(item,index) in showChooseListsOut">
                <div style="display: inline-block;width: 75%">
                    <el-row :gutter="1">
                        <el-col :span="24">
                            <el-form-item label="输出地址 :" prop="title" :label-width="formLabelWidth3">
                                <el-input size="small" v-model="item.address"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="1">
                        <el-col :span="24">
                            <el-form-item :label="'数量'+ (proParm ==='fch' ? 'f':'') + ':'" prop="title"
                                          :label-width="formLabelWidth3">
                                <el-input size="small" v-model="item.amount"></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </div>
                <div style="display: inline-block;width: 20%;text-align: center;vertical-align: top;margin-top: 5px">
                    <el-button class="block_btn" type="primary" size="small" @click="delOut(index)">删除</el-button>
                    <!--<i class="erweima" @click="erweimaClickOut(index)"></i>-->
                    <el-button v-if="index === showChooseListsOut.length-1" class="block_btn" style="margin-top: 32px;"
                               type="primary" size="small" @click="getResultClick(index)">全部
                    </el-button>
                </div>
                <div class="line"></div>
            </div>
            <el-row :gutter="1">
                <el-col :span="6" style="overflow-x: auto">
                    <el-form-item label="输入总量 :" :label-width="formLabelWidth1">
                        <span>{{inputTotal+(proParm ==='fch' ? 'f':'')}}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="6" style="overflow-x: auto">
                    <el-form-item label="输出总量 :" :label-width="formLabelWidth1">
                        <span>{{outputTotal+(proParm ==='fch' ? 'f':'')}}</span>
                    </el-form-item>
                </el-col>
                <!--<el-col :span="6" style="overflow-x: auto">-->
                    <!--<el-form-item label="找零 :" :label-width="formLabelWidth1">-->
                        <!--<span>{{smallChange+(proParm ==='fch' ? 'f':'')}}</span>-->
                    <!--</el-form-item>-->
                <!--</el-col>-->
                <!--<el-col :span="6" style="overflow-x: auto">-->
                    <!--<el-form-item label="矿工费 :" :label-width="formLabelWidth1">-->
                        <!--<span>{{Number((proParm ==='fch' ? serviceCharge*1000000 : serviceCharge).toFixed(8))+(proParm ==='fch' ? 'c':proParm)}}</span>-->
                    <!--</el-form-item>-->
                <!--</el-col>-->
            </el-row>
            <el-row :gutter="1">
                <el-col :span="24">
                    <el-form-item label="" prop="title" :label-width="formLabelWidth3">
                        <el-button type="primary" size="small" @click="addTransactionOut">添加交易输出</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <div class="line"></div>
           
              <el-form ref="ruleForm1" :model="ruleForm1"> 
              <el-row :gutter="1">
                <el-col :span="18">
                    <el-form-item label="附加信息 :" prop="userName" :label-width="formLabelWidth3">
                        <el-input v-model="ruleForm1.userName" size="small"></el-input>
                    </el-form-item>
                </el-col>
             </el-row>
             <el-row :gutter="1">
                <el-col :span="24" style="overflow-x: auto;white-space: nowrap">
                    <span style="line-height: 40px;font-size: 12px;color: #f00;padding-left: 72px">附加信息还可输入字节数:{{tishiAll}}</span>
                </el-col>
            </el-row>
    
            </el-form>
            <div class="line"></div>
            <el-row :gutter="1">
                <el-col :span="24">
                    <el-form-item label="" prop="title" >
                        <el-button type="primary" size="small" style="margin-left: 72px" @click="showAllewm">展示二维码</el-button>
                        <el-button size="small" type="primary"  @click="showQRMsg">生成待签名数据</el-button>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row :gutter="1" v-if="allMsg" style="margin-top: 20px">
                <el-col :span="24">
                    <el-input size="small" v-model="allMsg" type="textarea" :rows="5"></el-input>
                </el-col>
            </el-row>
        </el-form>


        <el-dialog title="" :visible.sync="dialogFormVisible" class="dialogFormVisible">
            <el-form :model="form">
                <el-checkbox-group v-model="form.checkList" @change="handleCheckedCitiesChange"
                                   style="height: 43vh;overflow: auto">
                    <div v-for="(item,index) in form.dataList">
                        <div style="display: inline-block;width: 85%">
                            <el-row :gutter="1">
                                <el-col :span="24">
                                    <el-form-item label="交易ID :" prop="txid" :label-width="formLabelWidth2">
                                        <el-input size="small" v-model="item.txid"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row :gutter="1">
                                <el-col :span="24">
                                    <el-form-item label="数量 :" prop="title" :label-width="formLabelWidth2">
                                        <el-input size="small" v-model="item.amount"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row :gutter="1">
                                <el-col :span="24">
                                    <el-form-item label="索引 :" prop="index" :label-width="formLabelWidth2">
                                        <el-input size="small" v-model="item.vout"></el-input>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                        </div>
                        <el-checkbox class="noName"
                                     style="width: 15%;display: inline-block;vertical-align: top;text-align: center;margin-top: 5px"
                                     :label="index"></el-checkbox>
                        <div class="line"></div>
                    </div>
                </el-checkbox-group>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-checkbox :indeterminate="isIndeterminate" size="small" border v-model="checkAll"
                             @change="handleCheckAllChange">全选
                </el-checkbox>
                <el-button type="primary" size="small" @click="choose">确 定</el-button>
            </div>
        </el-dialog>
        <el-dialog
            title=""
            :visible.sync="dialogVisible"
            width="240px"
            :before-close="handleClose">
            <EWMD ref="EWMdialog" :proParm="result"></EWMD>
        </el-dialog>
        <el-dialog
            title=""
            :visible.sync="qrCodeDialog"
            width="100%"
            :before-close="qrCodeContainerDestroy">
            <div v-if="qrCodeDestroy" style="display: flex; overflow: auto;">
                <div v-for="(item,index) in showChooseLists" class="qrContainer">
                    <div :id="'transactionInput'+index" class="qrContent"></div>
                    <div class="qrSign">{{'交易输入' + (index+1)+'/'+showChooseLists.length}}</div>
                    <div class="line"></div>
                    <div style="padding-bottom: 10px;text-align: center">当前/总数：{{index + 1}}/{{showChooseLists.length +
                        showChooseListsOut.length+userInfomsgs.length}}
                    </div>
                </div>
                <div v-for="(item,index) in showChooseListsOut" class="qrContainer">
                    <div :id="'transactionOut'+index" class="qrContent"></div>
                    <div class="qrSign">{{'交易输出' + (index+1)+'/'+showChooseListsOut.length}}</div>
                    <div class="line"></div>
                    <div style="padding-bottom: 10px;text-align: center">当前/总数：{{showChooseLists.length+(index +
                        1)}}/{{showChooseLists.length + showChooseListsOut.length+userInfomsgs.length}}
                    </div>
                </div>

            <div v-for="(item,index) in userInfomsgs" class="qrContainer">
                    <div :id="userInfo+index" class="qrContent"></div>
                    <div class="qrSign">{{'附加数据'+(index+1)+'/'+userInfomsgs.length}}</div>
                    <div class="line"></div>
                    <div style="padding-bottom: 10px;text-align: center">当前/总
数：{{showChooseLists.length+showChooseListsOut.length+(index + 1)}}/{{showChooseLists.length + userInfomsgs.length+showChooseListsOut.length}}</div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import Fetch from 'Fetch';
    import EWMD from './ewm';
    import ElForm from "../../node_modules/element-ui/packages/form/src/form.vue";

    const privatekey = 'KxhPaZzFT1S48C4mmZsBiAvxyAEE1E5zcnFKD93Zc69ENpchjxra';
    const txid = '54834c4aaf46679487c6663f30f8dd8644294ca4fecd98c2aca448a5e802e1d9';
    const inputamount = '0.01592572';
    const bchoutputaddre = 'bitcoincash:qqpclqmev2cv37f7k03nwrxd7c5an4c8k5v6jucvft';
    const fchoutputaddre='F69wQsgNXHXbc7WNuGLADdEgwCb2NhJuTx';
    const btcoutputaddre='1Kpx5FHfyJvywdM3ag1FEi9uYa1TtkdSa';
    
    const outputamount = '0.01192572';
    export default {
        name: "Transaction",
        components: {
            ElForm,
            EWMD
        },
        data() {
            return {
                loading: false,
                formLabelWidth1: '80px',
                formLabelWidth2: '80px',
                formLabelWidth3: '72px',
                formLabelWidth4: '100px',
                ruleForm: {
                    address: '',
                },
                rules: {
                    address: [
                        {required: true, message: '请输入地址', trigger: ['blur', 'change']}
                    ],
                },
                dialogFormVisible: false,
                showChooseLists: [],
                showChooseListsOut: [],
                form: {
                    dataList: [],
                    checkList: []
                },
                ruleForm1: {
                    userName: '',
                },
                dialogVisible: false,
                qrcode: null,
                result: null,
                isIndeterminate: false,
                checkAll: false,
                qrCodeDialog: false,
                qrCodeDestroy: false,
                smallChange: 0,
                serviceCharge: 0,
                inputTotal: 0,
                outputTotal: 0,
                allMsg: '',
                tishiAll:220,
                userInfomsgs: [],
                userInfoMsg: '',
                userInfo: 'userInfo'
            }
        },
        props: ['proParm'],
        computed: {
            inputTotalamount() {
                let total = 0;
                this.showChooseLists.forEach((item) => {
                    let amount = item.amount ? parseFloat(item.amount) : 0;
                    total = total + amount;
                });
                return total;
            },
            outputTotalamount() {
                let total = 0;
                this.showChooseListsOut.forEach((item) => {
                    let amount = item.amount ? parseFloat(item.amount) : 0;
                    total = total + amount;
                });
                return total;
            }
        },
        watch: {
            inputTotalamount(newVal, oldVal) {
                this.getResult();
            },
            outputTotalamount(newVal, oldVal) {
                this.getResult();
            },
             'ruleForm1.userName': function (val) {
               this.checkStrlength();
            }
            
        },
        methods: {
            async search() {
                this.$refs['ruleForm'].validate(async (valid) => {
                    if (valid) {
                        let obj = {};
                        obj.address = this.ruleForm.address;
                        obj.cointype = this.proParm;
                        this.form.checkList = [];
                        this.loading = true;
                        let result = await Fetch.Get('/wallet/listUtxo', obj);
                        if (result.code === '0') {
                            if (result.data.utxos.length > 0) {
                                this.dialogFormVisible = true;
                                this.form.dataList = result.data.utxos;
                            } else {
                                this.$message.info('该地址没有未花费的交易');
                            }
                        } else {
                            this.$message.error(result.msg);
                        }
                        this.loading = false;
                    } else {
                        return false;
                    }
                });
            },
            async searchNew() {
                this.$refs['ruleForm'].validate(async (valid) => {
                    if (valid) {
                        let obj = {};
                        obj.address = this.ruleForm.address;
                        obj.cointype = this.proParm;
                        this.form.checkList = [];
                        this.loading = true;
                        let result = await Fetch.Get('/wallet/listUtxo', obj);
                        if (result.code === '0') {
                            if (result.data.utxos.length > 0) {
                                this.dialogFormVisible = true;
                                this.form.dataList = result.data.utxos;
                                this.showChooseLists = [];
                                this.showChooseListsOut = [];
                            } else {
                                this.$message.info('该地址没有未花费的交易');
                            }
                        } else {
                            this.$message.error(result.msg);
                        }
                        this.loading = false;
                    } else {
                        return false;
                    }
                });
            },
            choose() {
                let self = this;
                let arr = [];
                self.form.checkList.forEach(item => {
                    arr.push(self.form.dataList[item]);
                });
                this.showChooseLists = this.showChooseLists.concat(arr);
                let newJson = [];
                for (const item1 of this.showChooseLists) {
                    let flag = true;
                    for (const item2 of newJson) {
                        if (item1.txid === item2.txid && item1.vout === item2.vout) {
                            flag = false;
                        }
                    }
                    if (flag) {
                        newJson.push(item1);
                    }
                }
                this.showChooseLists = newJson;
                this.dialogFormVisible = false;
            },
            del(index) {
                this.showChooseLists.splice(index, 1);
            },
            addTransactionIn() {
                this.showChooseLists.push({
                    address: '',
                    amount: '',
                    confirmations: '',
                    height: '',
                    satoshis: '',
                    txid: '',
                    vout: ''
                });
            },
            erweimaClick(index) {
                this.dialogVisible = true;
                let result = this.showChooseLists[index];
                result.dealType = 1;
                this.result = result;
                if (this.$refs.EWMdialog) {
                    this.$refs.EWMdialog.init(result);
                }
            },
            erweimaClickOut(index) {
                this.dialogVisible = true;
                let result = this.showChooseListsOut[index];
                result.dealType = 2;
                this.result = result;
                if (this.$refs.EWMdialog) {
                    this.$refs.EWMdialog.init(result);
                }
            },
            addTransactionOut() {
                this.showChooseListsOut.push({
                    address: '',
                    amount: ''
                });
            },
            delOut(index) {
                this.showChooseListsOut.splice(index, 1);
            },
            handleClose(done) {
                done();
            },
            handleCheckAllChange(val) {
                if (val) {
                    let array = [];
                    this.form.dataList.forEach((item, index) => {
                        array.push(index);
                    });
                    this.form.checkList = array;
                } else {
                    this.form.checkList = [];
                }
                this.isIndeterminate = false;
            },
            checkStrlength() {
                let userName_zj = stringToByte(this.ruleForm1.userName).length;
                this.tishiAll = 220 - userName_zj;
                if( this.tishiAll <= 0) {
                    this.tishiAll = 0;
                }
            },
            handleCheckedCitiesChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.form.dataList.length;
                this.isIndeterminate = checkedCount > 0 && checkedCount < this.form.dataList.length;
            },
            qrCodeContainerDestroy() {
                this.qrCodeDestroy = false;
                this.qrCodeDialog = false;
            },
            showAllewm() {
                let msg =this.ruleForm1.userName;
                let userName_zj = stringToByte(msg).length;
                if(userName_zj>220)
                {
                  this.$message.error('附加信息长度过长!');
                    return;
                } 
                if (this.showChooseLists.length === 0 && this.showChooseListsOut.length === 0) {
                    this.$message.error('请先添加交易输入或者交易输出!');
                    return;
                };
                if (this.outputTotal === 0 && this.inputTotal === 0) {
                    this.$message.error('请先点全部按钮计算输入总量和输出总量以及找零!');
                    return;
                }

                this.getJyf();

                let serviceCharge = this.serviceCharge;
                if (this.proParm === 'fch') {
                    serviceCharge = Number((this.serviceCharge * 1000000).toFixed(8)) + 'c';
                } else {
                    serviceCharge = this.serviceCharge + this.proParm;
                }
                let title = `输入总量：${this.inputTotal + (this.proParm === 'fch' ? 'f' : '')} 输出总量：${this.outputTotal + (this.proParm === 'fch' ? 'f' : '')}
                矿工费：${serviceCharge} 找零：${this.smallChange + (this.proParm === 'fch' ? 'f' : '')}`;
                this.$confirm(title, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    if (this.smallChange < 0) {
                        this.$message.error('输出总量大于了输入总量！');
                        return;
                    }
                    this.qrCodeDialog = true;
                    this.qrCodeDestroy = true;
                    this.$nextTick(() => {
                        this.showChooseLists.forEach((item, index) => {
                            let objCopy = Object.assign({}, item);
                            delete objCopy.confirmations;
                            delete objCopy.satoshis;
                            delete objCopy.height;
                            objCopy.dealType = 1;
                            // 复制原来的值
                            if (objCopy.dealType === 1) {
                                objCopy['index'] = objCopy.vout.toString();
                            }
                            objCopy.amount = objCopy.amount.toString();
                            delete objCopy.vout;
                            objCopy.seq = index + 1;
                            objCopy = JSON.stringify(objCopy);
                            let qrCode = new QRCode(document.getElementById('transactionInput' + index), {
                                width: 200,
                                height: 200
                            });
                            qrCode.makeCode(objCopy);
                        })
                        this.showChooseListsOut.forEach((item, index) => {
                            let objCopy = Object.assign({}, item);
                            objCopy.dealType = 2;
                            delete objCopy.confirmations;
                            delete objCopy.satoshis;
                            delete objCopy.height;
                            // 复制原来的值
                            if (objCopy.dealType === 1) {
                                objCopy['index'] = objCopy.vout.toString();
                            }
                            objCopy.amount = objCopy.amount.toString();
                            delete objCopy.vout;
                            objCopy.seq = index + 1;
                            objCopy = JSON.stringify(objCopy);
                            let qrCode = new QRCode(document.getElementById('transactionOut' + index), {
                                width: 200,
                                height: 200
                            });
                            qrCode.makeCode(objCopy);
                        });
 if(userName_zj>0)
{
                        let userInfo = {
                                dealType: 3,
                                msg: msg,
                                msgtype: 1
                            };
                    
                        this.userInfoMsg = JSON.stringify(userInfo);
                            this.userInfomsgs = this.userInfomsgsArry();
                            this.$nextTick(() => {
                                this.userInfomsgs.forEach((item, index) => {
                                    let userQrCode = new QRCode(document.getElementById(this.userInfo + index), {
                                        width: 200,
                                        height: 200
                                    });
                                    userQrCode.makeCode(item);
                                });
                            });
}

                    });
                }).catch(() => {
                });
            },userInfomsgsArry() {
                let length = this.userInfoMsg.length;
                let num = 1 //Math.ceil(length / 300);
                let array = [];
                for (let i = 0; i < 1; i++) {
                    let m = this.userInfoMsg;
                    array.push(m);
                }
                return array;
            },
            // 避免显示科学计数法
            toolNumber(num_str) {
                num_str = num_str.toString();
                if (num_str.indexOf("+") != -1) {
                    num_str = num_str.replace("+", "");
                }
                if (num_str.indexOf("E") != -1 || num_str.indexOf("e") != -1) {
                    var resValue = "",
                        power = "",
                        result = null,
                        dotIndex = 0,
                        resArr = [],
                        sym = "";
                    var numStr = num_str.toString();
                    if (numStr[0] == "-") {
                        // 如果为负数，转成正数处理，先去掉‘-’号，并保存‘-’.
                        numStr = numStr.substr(1);
                        sym = "-";
                    }
                    if (numStr.indexOf("E") != -1 || numStr.indexOf("e") != -1) {
                        var regExp = new RegExp(
                            "^(((\\d+.?\\d+)|(\\d+))[Ee]{1}((-(\\d+))|(\\d+)))$",
                            "ig"
                        );
                        result = regExp.exec(numStr);
                        if (result != null) {
                            resValue = result[2];
                            power = result[5];
                            result = null;
                        }
                        if (!resValue && !power) {
                            return false;
                        }
                        dotIndex = resValue.indexOf(".") == -1 ? 0 : resValue.indexOf(".");
                        resValue = resValue.replace(".", "");
                        resArr = resValue.split("");
                        if (Number(power) >= 0) {
                            var subres = resValue.substr(dotIndex);
                            power = Number(power);
                            //幂数大于小数点后面的数字位数时，后面加0
                            for (var i = 0; i <= power - subres.length; i++) {
                                resArr.push("0");
                            }
                            if (power - subres.length < 0) {
                                resArr.splice(dotIndex + power, 0, ".");
                            }
                        } else {
                            power = power.replace("-", "");
                            power = Number(power);
                            //幂数大于等于 小数点的index位置, 前面加0
                            for (var i = 0; i < power - dotIndex; i++) {
                                resArr.unshift("0");
                            }
                            var n = power - dotIndex >= 0 ? 1 : -(power - dotIndex);
                            resArr.splice(n, 0, ".");
                        }
                    }
                    resValue = resArr.join("");

                    return sym + resValue;
                } else {
                    return num_str;
                }
            },
            getResult() {
                // 输入总量
                let total = 0;
                this.showChooseLists.forEach((item) => {
                    let amount = item.amount ? parseFloat(item.amount) : 0;
                    total = total + amount;
                });
                this.inputTotal = Number(total.toFixed(8));
                // 输出总量
                let totalout = 0;
                this.showChooseListsOut.forEach((item) => {
                    let amount = item.amount ? parseFloat(item.amount) : 0;
                    totalout = totalout + amount;
                });
                this.outputTotal = Number(totalout.toFixed(8));
            },

            // 交易费计算
            getJyf() {
                let privatekeys = [];
                let txids = [];
                let inputamounts = [];
                let indexs = [];
                let outputaddres = [];
                let outputamounts = [];
                let outputaddre='';
                let returnaddr='';
                 
                if(this.proParm=="btc")
                {
                  outputaddre=btcoutputaddre;
                  returnaddr=btcoutputaddre;
                }
                
                if(this.proParm=="fch")
                {
                  outputaddre=fchoutputaddre;
                  returnaddr=fchoutputaddre;
                }
                
                if(this.proParm=="bch")
                {
                  outputaddre=bchoutputaddre;
                  returnaddr=bchoutputaddre;
                }
                this.showChooseLists.forEach((item, index) => {
                    privatekeys.push(privatekey);
                    txids.push(txid);
                    inputamounts.push(inputamount);
                    indexs.push(index);
                });
                this.showChooseListsOut.forEach((item, index) => {
                    outputaddres.push(outputaddre);
                    outputamounts.push(outputamount);
                });
                let s = calcMinTranscationFee(this.proParm, privatekeys, txids, inputamounts, indexs, outputaddres, outputamounts, returnaddr) / 100000000;
                this.serviceCharge = Number(s.toFixed(8));

                let ss = Number((this.inputTotal - this.outputTotal - this.serviceCharge).toFixed(8));
                this.smallChange = this.toolNumber(ss);
            },

            // 全部找零
            getResultQb() {
                // 输入总量
                let total = 0;
                this.showChooseLists.forEach((item) => {
                    let amount = item.amount ? parseFloat(item.amount) : 0;
                    total = total + amount;
                });
                let inputTotal = Number(total.toFixed(8));

                let ouarry = [].concat(this.showChooseListsOut);

                ouarry[ouarry.length-1].amount = 0;

                // 输出总量
                let totalout = 0;
                ouarry.forEach((item) => {
                    let amount = item.amount ? parseFloat(item.amount) : 0;
                    totalout = totalout + amount;
                });
                let outputTotal = Number(totalout.toFixed(8));
                // 交易费计算
                let privatekeys = [];
                let txids = [];
                let inputamounts = [];
                let indexs = [];
                let outputaddres = [];
                let outputamounts = [];
                
                let outputaddre='';
                let returnaddr='';
                 
                if(this.proParm=="btc")
                {
                  outputaddre=btcoutputaddre;
                  returnaddr=btcoutputaddre;
                }
                
                if(this.proParm=="fch")
                {
                  outputaddre=fchoutputaddre;
                  returnaddr=fchoutputaddre;
                }
                
                if(this.proParm=="bch")
                {
                  outputaddre=bchoutputaddre;
                  returnaddr=bchoutputaddre;
                }
                
                this.showChooseLists.forEach((item, index) => {
                    privatekeys.push(privatekey);
                    txids.push(txid);
                    inputamounts.push(inputamount);
                    indexs.push(index);
                });
                ouarry.forEach((item, index) => {
                    outputaddres.push(outputaddre);
                    outputamounts.push(outputamount);
                });
                let s = calcMinTranscationFee(this.proParm, privatekeys, txids, inputamounts, indexs, outputaddres, outputamounts, returnaddr) / 100000000;
                let serviceCharge = Number(s.toFixed(8));
                return Number((inputTotal - outputTotal - serviceCharge).toFixed(8));
            },
            getResultClick(index) {
                this.showChooseListsOut[index].amount = this.getResultQb();
            },

            showQRMsg() {
                if (this.showChooseLists.length === 0 && this.showChooseListsOut.length === 0) {
                    this.$message.error('请先添加交易输入或者交易输出!');
                    return;
                };
                if (this.outputTotal === 0 && this.inputTotal === 0) {
                    this.$message.error('请先点全部按钮计算输入总量和输出总量以及找零!');
                    return;
                }
                this.getJyf();
                if (this.smallChange < 0) {
                    this.$message.error('输出总量大于了输入总量！');
                    return;
                }
                this.allMsg = '';
                let arr = [];

                let inputArr = [];
                this.showChooseLists.forEach((item, index) => {
                    let objCopy = Object.assign({}, item);
                    delete objCopy.confirmations;
                    delete objCopy.satoshis;
                    delete objCopy.height;
                    objCopy.dealType = 1;
                    // 复制原来的值
                    if (objCopy.dealType === 1) {
                        objCopy['index'] = objCopy.vout.toString();
                    }
                    objCopy.amount = objCopy.amount.toString();
                    delete objCopy.vout;
                    objCopy.seq = index + 1;
                    inputArr.push(objCopy);
                });
                let outArr = [];
                this.showChooseListsOut.forEach((item, index) => {
                    let objCopy = Object.assign({}, item);
                    objCopy.dealType = 2;
                    delete objCopy.confirmations;
                    delete objCopy.satoshis;
                    delete objCopy.height;
                    // 复制原来的值
                    if (objCopy.dealType === 1) {
                        objCopy['index'] = objCopy.vout.toString();
                    }
                    objCopy.amount = objCopy.amount.toString();
                    delete objCopy.vout;
                    objCopy.seq = index + 1;
                    outArr.push(objCopy);
                })
                let msg =this.ruleForm1.userName;
                let userName_zj = stringToByte(msg).length;
                if(userName_zj>220)
                {
                  this.$message.error('附加信息长度过长!');
                    return;
                }

                let userInfo = {
                                dealType: 3,
                                msg: msg,
                                msgtype: 1
                            };
                if(userName_zj>0)
                {
                arr = arr.concat(inputArr,outArr,userInfo);
                }else
                {
                 
                arr = arr.concat(inputArr,outArr);
                  } 
               this.allMsg = JSON.stringify(arr);

            }
        }
    }
</script>

<style scoped lang="less">

    .title {
        font-weight: bold;
    }

    /deep/ .el-form-item__label {
        padding: 0 5px 0 0;
    }

    .block_btn {
        margin-left: 10px;
        background-color: red;
        display: block;
        border: solid 1px red;
    }

    .noName /deep/ .el-checkbox__label {
        display: none
    }

    .indexCode {
        display: inline-block;
        height: 30px;
        margin-top: 32px;
        margin-left: 20px;
        line-height: 32px;
        font-size: 12px;
    }

    .erweima {
        width: 30px;
        height: 30px;
        background: url("../../static/images/erweima.png");
        background-size: cover;
        display: block;
        margin-top: 32px;
        margin-left: 20px;
    }

    .line {
        width: 100%;
        height: 5px;
        margin: 5px 0;
        background-color: #e4e7ed;
        z-index: 1;
    }

    .qrContainer {
        display: inline-block;
    }

    .qrContent {
        display: inline-block;
        width: 200px;
        height: 200px;
        margin: 10px;
    }

    .qrSign {
        width: 200px;
        height: 30px;
        line-height: 30px;
        text-align: center;
    }
</style>
